package com.itheima.controller;

import com.itheima.pojo.Result;
import com.itheima.utils.AliOSSUtils;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * 处理文件上传
 */
@Slf4j
@RestController
@RequestMapping("upload")
public class UploadController {
    @Autowired
    private AliOSSUtils aliOSSUtils;

//    @PostMapping("/upload")
//    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
//        log.info("文件上传：{},{},{}",username,age,image);
//
//        //获取原始文件名
//        String originalFilename = image.getOriginalFilename();
//
//        //构建新的文件名
//        String extname = originalFilename.substring(originalFilename.lastIndexOf("."));//文件扩展名
//        String newFileName = UUID.randomUUID().toString()+extname;//随机名+文件扩展名
//
//        //将文件存储在服务器的磁盘目录
//        image.transferTo(new File("D:/image/"+newFileName));
//
//        return Result.success();
//    }
    //LTAI5tH6FyrfHDmM9Hhq1yH4
    //LCbaVTggRp6Nn1nvC7i9ExXfYXpQil

    @PostMapping
    @SneakyThrows //抑制异常
    public Result upload(MultipartFile image) {
        log.info("临时文件 {}", image);
        //调用AliOSSutils，一定要注意，返回的是oss中图片的访问地址
        String url = aliOSSUtils.upload(image);
        return Result.success(url);
    }


}
